<template>
  <page-header-wrapper>

    <div>
      <a-row :gutter="24">
        <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="操作"
            :body-style="{ padding: 0 }">
            <a slot="extra">全部项目</a>
            <div>
              <a-card-grid class="project-card-grid">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a style="font-size: 16px">顾客到店充值</a>
                    </div>
                  </a-card-meta>
                  <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 10 }" style="margin-top: 50px;height:168px">
                    <a-form-item label="用户手机号">
                      <a-input v-model:value="phone.v1"/>
                    </a-form-item>
                    <a-form-item label="充值金额">
                      <a-input-number v-model:value="infos.num" :min="1" :precision="1" style="width: 100%"/>
                    </a-form-item>
                    <a-form-item :wrapper-col="{ span: 8, offset: 8 }">
                      <a-button type="primary" block @click="v1submit">提交</a-button>
                    </a-form-item>
                  </a-form>

                  <a-modal
                    title="确定充值？"
                    :visible="state.v1"
                    @ok="v1handleOk"
                    @cancel="state.v1=false"
                  >
                    <p>确定充值？</p>
                  </a-modal>


                </a-card>
              </a-card-grid>
              <a-card-grid class="project-card-grid">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a style="font-size: 16px">场次预定确认</a>
                    </div>
                  </a-card-meta>
                  <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 10 }" style="margin-top: 50px;height:168px">

                    <a-form-item label="使用状态">
                      <a-select v-model="infos.type" placeholder="请选择" default-value="">
                        <a-select-option value="">全部</a-select-option>
                        <a-select-option value="2">半场</a-select-option>
                        <a-select-option value="3">全场</a-select-option>
                      </a-select>
                    </a-form-item>

                    <a-form-item label="用户手机号">
                      <a-input v-model:value="phone.v2"/>
                    </a-form-item>

                    <a-form-item :wrapper-col="{ span: 8, offset: 8 }">
                      <a-button type="primary" block @click="v2submit" >提交</a-button>
                    </a-form-item>
                  </a-form>

                  <a-modal
                    title="该客户预定的场次"
                    :visible="state.v2"
                    :closable="false"
                    @ok="state.v2=false" width="900px"
                    :cancelButtonProps="{style:'display:none'}">

                    <a-table :columns="columns" :data-source="infos.orderDate" size="small" :scroll="{  y: 330 }">
                      <div slot="expandedRowRender" slot-scope="record" style="margin: 0">
                        <div slot="header" style="margin-bottom: 10px">
                          预定时段
                        </div>
                        <a-list size="small" bordered :data-source="record.calendarId">
                          <a-list-item slot="renderItem" slot-scope="item, index">
                            {{ orderTime[item-1].text }}
                          </a-list-item>
                        </a-list>
                      </div>
                      <span slot="action" slot-scope="text, record">
                        <a @click="enSureOrder(record)">确定订单</a>
                      </span>
                      <span slot="state" slot-scope="text">
                        {{text===1?'未付款':(text===2?'已付款':(text===3?'已完成':'已取消'))}}
                      </span>
                    </a-table>
                  </a-modal>


                </a-card>
              </a-card-grid>
              <a-card-grid class="project-card-grid">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a style="font-size: 16px">散客到店确认</a>
                    </div>
                  </a-card-meta>
                  <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 10 }" style="margin-top: 113px;height:105px">
                    <a-form-item label="用户手机号">
                      <a-input v-model="phone.v3"/>
                    </a-form-item>

                    <a-form-item :wrapper-col="{ span: 8, offset: 8 }">
                      <a-button type="primary" block @click="v3submit">提交</a-button>
                    </a-form-item>
                  </a-form>
                  <a-modal
                    :visible="state.v3"
                    @ok="v3handleOk"
                    @cancel="state.v3=false"
                  >
                    <h1>确定到店？</h1>
                  </a-modal>
                </a-card>
              </a-card-grid>
              <a-card-grid class="project-card-grid">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a style="font-size: 16px">顾客到店消费</a>
                    </div>
                  </a-card-meta>
                  <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 10 }" style="margin-top: 50px;height:168px">
                    <a-form-item label="用户手机号">
                      <a-input v-model="phone.v4"/>
                    </a-form-item>
                    <a-form-item label="消费金额">
                      <a-input-number v-model:value="infos.num1" :min="1" :precision="1" style="width: 100%"/>
                    </a-form-item>
                    <a-form-item label="备注(不可为空)">
                      <a-input v-model="infos.description"/>
                    </a-form-item>

                    <a-form-item :wrapper-col="{ span: 8, offset: 8 }">
                      <a-button type="primary" block @click="v4submit">提交</a-button>
                    </a-form-item>
                  </a-form>

                  <a-modal
                    :visible="state.v4"
                    @ok="v4handleOk"
                    @cancel="state.v4=false"
                  >
                    <h1>确定消费{{infos.num1}}个跃动币？</h1>
                  </a-modal>

                </a-card>
              </a-card-grid>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </page-header-wrapper>
</template>

<script>
    import {timeFix} from '@/utils/util'
    import {mapState} from 'vuex'
    import {PageHeaderWrapper} from '@ant-design-vue/pro-layout'
    import {Radar} from '@/components'
    import {topUpWallet,useTicket,useBalance} from "../../api/wallet";
    import {orderHistory} from "../../api/history";
    import {ensureOrder} from "../../api/history";

    const DataSet = require('@antv/data-set')
    // customRender

    export default {
        name: 'Workplace',
        components: {
            PageHeaderWrapper,
            Radar
        },
        data() {
            return {
                timeFix: timeFix(),
                avatar: '',
                user: {},
                infos: {
                    num: 1,
                    num1:1,
                    type: null,
                    orderDate: [],
                    description:''
                },
                columns: [

                    {
                        dataIndex: 'id',
                        key: 'id',
                        title: 'id',
                    },
                    {
                        title: '场地',
                        dataIndex: 'courtId',
                        key: 'courtId',
                        customRender(text) {
                            let orderPosition = [
                                {
                                    "value": 1,
                                    "text": "场地 1"
                                },
                                {
                                    "value": 2,
                                    "text": "场地 2"
                                },
                                {
                                    "value": 3,
                                    "text": "场地 3"
                                },
                                {
                                    "value": 4,
                                    "text": "场地 4"
                                }, {
                                    "value": 5,
                                    "text": "场地 1+场地 2"
                                },
                                {
                                    "value": 6,
                                    "text": "场地 3+场地 4"
                                }
                            ]
                            return orderPosition[Number(text) - 1].text
                        }
                    },
                    {
                        title: '预定类型',
                        dataIndex: 'type',
                        key: 'type',
                        customRender(text) {
                            let a = ['', '半场', '全场']
                            return a[Number(text)-1]
                        }
                    },
                    {
                        title: '状态',
                        dataIndex: 'state',
                        scopedSlots: {customRender: 'state'},
                    },
                    {
                        title: '操作',
                        key: 'action',
                        scopedSlots: {customRender: 'action'},
                    },

                ],
                orderTime: [
                    {
                        "value": 1,
                        "text": "10:00~11:00",
                        "disable": true
                    },
                    {
                        "value": 2,
                        "text": "11:00~12:00",
                        "disable": true
                    },
                    {
                        "value": 3,
                        "text": "12:00~13:00",
                        "disable": true
                    },
                    {
                        "value": 4,
                        "text": "13:00~14:00",
                        "disable": true
                    },
                    {
                        "value": 5,
                        "text": "14:00~15:00",
                        "disable": true
                    },
                    {
                        "value": 6,
                        "text": "15:00~16:00",
                        "disable": true
                    },
                    {
                        "value": 7,
                        "text": "16:00~17:00",
                        "disable": true
                    },
                    {
                        "value": 8,
                        "text": "17:00~18:00",
                        "disable": true
                    },
                    {
                        "value": 9,
                        "text": "18:00~19:00",
                        "disable": true
                    },
                    {
                        "value": 10,
                        "text": "19:00~20:00",
                        "disable": true
                    },
                    {
                        "value": 11,
                        "text": "20:00~21:00",
                        "disable": true
                    },
                    {
                        "value": 12,
                        "text": "21:00~22:00",
                        "disable": true
                    }
                ],
                orderPosition: [
                    {
                        "value": 1,
                        "text": "场地 1"
                    },
                    {
                        "value": 2,
                        "text": "场地 2"
                    },
                    {
                        "value": 3,
                        "text": "场地 3"
                    },
                    {
                        "value": 4,
                        "text": "场地 4"
                    }, {
                        "value": 5,
                        "text": "场地 1+场地 2"
                    },
                    {
                        "value": 6,
                        "text": "场地 3+场地 4"
                    }
                ],
                phone: {
                    v1: '',
                    v2: '',
                    v3: '',
                    v4: '',
                    v5: '',
                    v6: ''
                },
                state: {
                    v1: false,
                    v2: false,
                    v3: false,
                    v4: false,
                    v5: false,
                    v6: false
                },
                projects: [],
                loading: true,
                radarLoading: true,
                activities: [],
                teams: [],

                radarData: [],
                info: []
            }
        },
        computed: {
            ...mapState({
                nickname: (state) => state.user.nickname,
                welcome: (state) => state.user.welcome
            }),
            currentUser() {
                return {
                    name: 'Serati Ma',
                    avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png'
                }
            },
            userInfo() {
                return this.$store.getters.userInfo
            }
        },
        created() {
            this.user = this.userInfo
            this.avatar = this.userInfo.avatar

        },
        mounted() {
        },
        methods: {
            v1submit() {
                let _this = this;
                if (this.phone.v1.length !== 11) {
                    _this.$notification.warning({
                        message: '请确定手机格式输入正确'
                    })
                } else if (_this.infos.num < 1) {
                    _this.$notification.warning({
                        message: '请确定金额输入正确'
                    })
                } else {
                    _this.state.v1 = true
                }
            },
            v1handleOk() {
                let _this = this;
                topUpWallet({
                    phone: _this.phone.v1,
                    price: _this.infos.num
                }).then((res) => {
                    if (res.status === 200) {
                        _this.$notification.success({
                            message: '充值成功'
                        })
                    } else {
                        _this.$notification.warning({
                            message: res.message
                        })
                    }
                }).catch((res) => {
                    _this.$notification.warning({
                        message: '充值失败'
                    })
                })

                _this.state.v1 = false;
            },
            v2submit(){
                let _this=this
                if (_this.phone.v2.length !== 11) {
                    _this.$notification.warning({
                        message: '请确定手机格式输入正确'
                    })
                    return
                }
                (async ()=>{
                    await _this.getUserOrderList(_this);
                    _this.state.v2 = true
                })()


            },
            getUserOrderList:async (_this)=>{
                if (_this.phone.v2.length !== 11) {
                    _this.$notification.warning({
                        message: '请确定手机格式输入正确'
                    })
                } else {
                    let date = new Date()
                    await orderHistory({
                        'user.phone': _this.phone.v2,
                        'type': _this.infos.type,
                        'date': date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
                    }).then((res) => {
                        if (res.data == null) {
                            _this.$notification.warning({
                                message: '输入的手机号不正确或无该用户'
                            })
                        } else {
                            _this.infos.orderDate = res.data.records;
                            for (let i = 0; i < _this.infos.orderDate.length; i++) {
                                _this.infos.orderDate[i].calendarId = JSON.parse(_this.infos.orderDate[i].calendarId)
                            }
                        }
                        console.log(res)
                    })
                }
            },
            enSureOrder(re){
                console.log(re.id)
                let _this=this;
                ensureOrder({
                    id:re.id
                }).then(res=>{
                    console.log(res)
                    if(res.status===200){
                        _this.$notification.success({
                            message:'确认成功'
                        })
                        _this.getUserOrderList(_this)
                    }else{
                        _this.$notification.warning({
                            message:res.message
                        })
                    }
                }).catch(res=>{
                    _this.$notification.warning({
                        message:res.message
                    })
                })
            },
            v3submit(){
                let _this = this;
                if (this.phone.v3.length !== 11) {
                    _this.$notification.warning({
                        message: '请确定手机格式输入正确'
                    })
                } else {
                    _this.state.v3 = true
                }
            },
            v3handleOk(){
                let _this = this;
                useTicket({
                    phone:_this.phone.v3,
                    ticketNum:1
                }).then((res) => {
                    if (res.status === 200) {
                        _this.$notification.success({
                            message: '消耗成功'
                        })
                    } else {
                        _this.$notification.warning({
                            message: res.message
                        })
                    }
                }).catch((res) => {
                    _this.$notification.warning({
                        message: res.message
                    })
                })

                _this.state.v3 = false;
            },
            v4submit(){
                let _this = this;
                if (this.phone.v4.length !== 11) {
                    _this.$notification.warning({
                        message: '请确定手机格式输入正确'
                    })
                } else {
                    _this.state.v4 = true
                }
            },
            v4handleOk(){
                let _this = this;
                useBalance({
                    phone:_this.phone.v4,
                    sum:_this.infos.num1,
                    description:_this.infos.description
                }).then((res) => {
                    if (res.status === 200) {
                        _this.$notification.success({
                            message: '消耗成功'
                        })
                    } else {
                        _this.$notification.warning({
                            message: res.message
                        })
                    }
                }).catch((res) => {
                    _this.$notification.warning({
                        message: res.message
                    })
                })

                _this.state.v4 = false;
            }
        }
    }
</script>

<style lang="less" scoped>
  @import "./Workplace.less";

  .project-list {

    .card-title {
      font-size: 0;
      text-align: center;

      a {
        color: rgba(0, 0, 0, 0.85);
        margin-left: 12px;
        line-height: 24px;
        height: 24px;
        display: inline-block;
        vertical-align: top;
        font-size: 14px;
        text-align: center;

        &:hover {
          color: #1890ff;
        }
      }
    }

    .card-description {
      color: rgba(0, 0, 0, 0.45);
      height: 44px;
      line-height: 22px;
      overflow: hidden;
    }

    .project-item {
      display: flex;
      margin-top: 8px;
      overflow: hidden;
      font-size: 12px;
      height: 20px;
      line-height: 20px;

      a {
        color: rgba(0, 0, 0, 0.45);
        display: inline-block;
        flex: 1 1 0;

        &:hover {
          color: #1890ff;
        }
      }

      .datetime {
        color: rgba(0, 0, 0, 0.25);
        flex: 0 0 auto;
        float: right;
      }
    }

    .ant-card-meta-description {
      color: rgba(0, 0, 0, 0.45);
      height: 44px;
      line-height: 22px;
      overflow: hidden;
    }
  }

  .item-group {
    padding: 20px 0 8px 24px;
    font-size: 0;

    a {
      color: rgba(0, 0, 0, 0.65);
      display: inline-block;
      font-size: 14px;
      margin-bottom: 13px;
      width: 25%;
    }
  }

  .members {
    a {
      display: block;
      margin: 12px 0;
      line-height: 24px;
      height: 24px;

      .member {
        font-size: 14px;
        color: rgba(0, 0, 0, .65);
        line-height: 24px;
        max-width: 100px;
        vertical-align: top;
        margin-left: 12px;
        transition: all 0.3s;
        display: inline-block;
      }

      &:hover {
        span {
          color: #1890ff;
        }
      }
    }
  }

  .mobile {

    .project-list {

      .project-card-grid {
        width: 100%;
      }
    }

    .more-info {
      border: 0;
      padding-top: 16px;
      margin: 16px 0 16px;
    }

    .headerContent .title .welcome-text {
      display: none;
    }
  }

</style>
